package com.chrray.position.exception;

import com.chrray.position.util.result.ResponseResult;
import com.chrray.position.util.result.ResponseResults;
import com.chrray.position.util.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @BelongsProject: financial
 * @BelongsPackage: com.yhr.manager.error
 * @Author: yang
 * @CreateTime: 2019-03-26 13:27
 * @Description: 统一错误处理
 * e 里的参数可以指定增强控制器要扫描的包，如果制定了 只对这个包产生作用，不指定，是对全局抛出的错误都产生作用
 * @ControllerAdvice("com.kb.controller")
 */
@ControllerAdvice()
@Slf4j
public class ErrorControllerAdvice {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult handleException(Exception e) {
        String msg = e.getMessage();
        if (e instanceof HttpRequestMethodNotSupportedException) {
            log.error("请求方式错误：", e);
            return ResponseResults.fail(ResultEnum.METHOND_NOT_ALLOWED);
        } else if (e instanceof NullPointerException) {
            if (msg != null) {
                return ResponseResults.fail(ResultEnum.MUST_PARAMETER, msg);
            }
            log.error("缺少必填参数：", e);
            return ResponseResults.fail(ResultEnum.MUST_PARAMETER, "缺少必填参数");
        } else if (e instanceof BindException) {
            log.error("参数格式错误：", e);
            return ResponseResults.fail(ResultEnum.BAD_REQUEST, "参数格式错误");
        } else if (e instanceof MyBatisSystemException) {
            log.error("sql执行错误：", e);
            return ResponseResults.fail(ResultEnum.SERVER_ERR, "sql执行错误");
        } else if (e instanceof MethodArgumentNotValidException) {
            return ResponseResults.fail(ResultEnum.BAD_REQUEST, ((MethodArgumentNotValidException) e).getBindingResult().getFieldError().getDefaultMessage());
        } else if (e instanceof BadSqlGrammarException) {
            log.error("数据库错误：", e);
            return ResponseResults.fail(ResultEnum.SERVER_ERR, "服务器内部错误");
        }else if(e instanceof FailException){
            if(msg!=null){
                return ResponseResults.fail(ResultEnum.SERVER_ERR, msg);
            }
            log.error("操作失败：",e);
            return ResponseResults.fail(ResultEnum.SERVER_ERR, "操作失败");
        } else if(e instanceof ServiceException){
            if(msg!=null){
                return ResponseResults.fail(ResultEnum.SERVER_ERR, msg);
            }
            log.error("操作失败：",e);
            return ResponseResults.fail(ResultEnum.SERVER_ERR, "操作失败");
        } else if(e instanceof IllegalArgumentException) {
            if (msg != null) {
                return ResponseResults.fail(ResultEnum.BAD_REQUEST, msg);
            }
            log.error("参数错误：", e);
            return ResponseResults.fail(ResultEnum.BAD_REQUEST, "参数错误");
        }
        log.error("其他异常：", e);
        return ResponseResults.fail(ResultEnum.BAD_REQUEST);
    }
}

